package com.mall.controller;

import com.mall.pageResult.PageResult;
import com.mall.pojo.UmsMenu;
import com.mall.pojo.UmsResource;
import com.mall.pojo.UmsRole;
import com.mall.service.RoleService;
import com.mall.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("/listAll")
    public Result<List<UmsRole>> listAll(){
        List<UmsRole> result = roleService.listAll();
        return Result.success(result,"操作成功");
    }

    /**
     * 根据角色名称分页获取角色列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("list")
    public Result<PageResult<UmsRole>> list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                            @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                            @RequestParam(required = false) String keyword){
        PageResult<UmsRole> result = roleService.list(pageNum,pageSize,keyword);
        return Result.success(result,"操作成功");
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/create")
    public Result<Integer> create(@RequestBody UmsRole role){
        Integer count = roleService.create(role);
        return Result.success(count,"操作成功");
    }

    /**
     * 修改角色
     * @param id
     * @param role
     * @return
     */
    @PostMapping("/update/{id}")
    public Result<Integer> update(@PathVariable Integer id,@RequestBody UmsRole role){
        Integer count = roleService.update(id,role);
        return Result.success(count,"操作成功");
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result<Integer> delete(Integer ids){
        Integer count = roleService.delete(ids);
        return Result.success(count,"操作成功");
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public Result<Integer> updateStatus(@PathVariable Integer id, Integer status){
        Integer count = roleService.updateStatus(id,status);
        return Result.success(count,"操作成功");
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public Result<List<UmsMenu>> listMenu(@PathVariable Integer roleId){
        List<UmsMenu> result = roleService.listMenu(roleId);
        return Result.success(result,"操作成功");
    }

    /**
     * 给角色分配菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("allocMenu")
    public Result<Integer> allocMenu(Integer roleId, Integer[] menuIds){
        Integer count = roleService.allocMenu(roleId,menuIds);
        return Result.success(count,"操作成功");
    }

    /**
     * 获取角色相关资源
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public Result<List<UmsResource>> listResource(@PathVariable Integer roleId){
        List<UmsResource> resources = roleService.listResource(roleId);
        return Result.success(resources,"操作成功");
    }

    /**
     * 给角色分配资源
     * @param roleId
     * @param resourceIds
     * @return
     */
    @PostMapping("/allocResource")
    public Result<Integer> allocResource(Integer roleId, Integer[] resourceIds){
        Integer count = roleService.allocResource(roleId,resourceIds);
        return Result.success(count,"操作成功");
    }
}
